var padDate = function(value){
    return value<10? '0'+value:value;

};

Vue.filter('date', time => moment(time).format('DD/MM/YY, HH:mm'));
// New VueJS instance
new Vue({
  name: 'notebook',
  el: '#notebook',

  // Some data
  data () {
    return {
     // content: localStorage.getItem('content') || 'You can write in **markdown**',
      notes: JSON.parse(localStorage.getItem('notes')) || [],
      selectedId: localStorage.getItem('selected-id') || null,
      //date: new Date()
    }
  },

  // created(){
  //   this.content = localStorage.getItem('content') || 'You can write in **markdown**'
  // },
  computed:{
    notePreview: function(){
      return  this.selectedNote ? marked(this.selectedNote.content) : '' ;
    },
    selectedNote: function(){
      return this.notes.find(note => note.id === this.selectedId);
    },
    sortedNotes(){
      return this.notes.slice().sort((a,b) => a.created - b.created).sort((a,b) => (a.favorite === b.favorite) ? 0 : a.favorite ? -1 :1);
    },
    linesCount(){
      if(this.selectedNote){
        return this.selectedNote.content.split(/\r\n|\r|\n/).length;
      }
    },
    wordsCount(){
      if(this.selectedNote){
        var s = this.selectedNote.content;
        s=s.replace(/\n/g, ' ');
        s=s.replace(/(^\s*)|(\s*$)/gi,'');
        s=s.replace(/\s\s+/gi,' ');
        return s.split(' ').length;
      }
    },

    charactersCount(){
      if(this.selectedNote){
        return this.selectedNote.content.split('').length;
      }
    }
  },

   watch:{
  //   content:{
  //     handler:'saveNote'
  //   }
      notes: {
        handler:'saveNotes',
        deep:true
      },

      selectedId(val){
        localStorage.setItem('selected-id',val);
      }
   },

  methods:{
    // saveNote(val,oldVal){
    //   console.log('new note:',val,'old note:',oldVal);
    //   console.log('saving note:',val);
    //   localStorage.setItem('content',val);
    //   this.reportOperation('saving');
    // },
    // reportOperation(opName){
    //   console.log('The',opName,'operation wa completed')
    // },
    addNote(){
	  var time = Date.now();
      const note = {
        id: String(time),
        title: 'New Note' + (this.notes.length + 1),
        content: '**Hi!** This notebook is using [markdown](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) for formatting!',
        created: new Date(),
        favorite: false
      };
      this.notes.push(note);
    },
    selectNote(note){
      this.selectedId= note.id;
    },

    saveNotes(){
      localStorage.setItem('notes',JSON.stringify(this.notes));
      console.log('Notes saved!', new Date());
    },

    removeNote(){
      if(this.selectedNote && confirm('Delete the note?')){
        const index = this.notes.indexOf(this.selectedNote);
        if(index!== -1){
          this.notes.splice(index,1);
        }
      }
    },

    favoriteNote(){
      this.selectedNote.favorite = !this.selectedNote.favorite;
    }
  }
});

//console.log('restored note:',localStorage.getItem('content'));
